import { getUserPermission, getUserInfo } from '@/api/user.js';

const state = {
    userInfo: {},
    userPermission: [],
    modulesPermission: [],
    menusPermission: [],
    functionsPermission: [],
    buttonsPermission: {},
  },
  mutations = {
    SET_USERINFO: (state, obj) => {
      state.userInfo = obj;
    },
    SET_PERMISSION: (state, list) => {
      state.userPermission = list;
    },
    setModulesPermission: (state, list) => {
      state.modulesPermission = list;
    },
    setMenusPermission: (state, list) => {
      state.menusPermission = list;
    },
    setFunctionsPermission: (state, list) => {
      state.functionsPermission = list;
    },
    setButtonsPermission: (state, obj) => {
      state.buttonsPermission = obj;
    },
  },
  actions = {
    async getUserInfo({ commit }) {
      await getUserInfo().then(res => {
        if (res.code == 200) {
          commit('SET_USERINFO', res.data);
        }
      });
    },
    async getUserPermission({ commit }) {
      return await getUserPermission().then(res => {
        if (res.code == 200) {
          commit('SET_PERMISSION', res.data);
          return res.data;
        }
        return [];
      });
    },
  };


export default {
  namespaced: true,
  state,
  mutations,
  actions
};
